package ee.ut.math.tvt.salessystem.ui.dialog;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import ee.ut.math.tvt.salessystem.domain.data.SoldItem;
import ee.ut.math.tvt.salessystem.listener.KeyReleasedListener;
import ee.ut.math.tvt.salessystem.ui.model.SalesSystemModel;
import ee.ut.math.tvt.swing.JDoubleField;

/**
 * @author Margus Hanni
 *
 */
@SuppressWarnings("serial")
public class ConfirmDialog extends JDialog {

	private JLabel sumLabel = new JLabel("Total");
	private JLabel sumShowLabel = new JLabel();

	private JLabel paySumLabel = new JLabel("Paid");
	private JDoubleField paySumField = new JDoubleField();

	private JLabel backLabel = new JLabel("Cash back");
	private JLabel backShowLabel = new JLabel(NumberFormat.getCurrencyInstance().format(0));

	private JButton confirmButton = new JButton("Confirm");
	private JButton cancelButton = new JButton("Cancel");

	private boolean cancel = true;

	final double sumTotal;

	public ConfirmDialog(Frame owner, SalesSystemModel salesSystemModel, boolean modal) {
		super(owner, modal);

		double sumTotalCount = 0.0;

		for (SoldItem soldItem : salesSystemModel.getCurrentPurchaseTableModel().getTableRows()) {
			sumTotalCount += soldItem.getSum();
		}
		sumTotal = sumTotalCount;

		init();
	}

	private void init() {

		this.setTitle("Confirm");
		this.setLayout(new GridLayout(4, 2));

		this.add(sumLabel);
		sumShowLabel.setText(NumberFormat.getCurrencyInstance().format(sumTotal));
		this.add(sumShowLabel);

		this.add(paySumLabel);
		paySumField.addKeyListener(new KeyReleasedListener() {

			@Override
			public void keyReleased(KeyEvent e) {
				if (paySumField.getValue() > sumTotal) {
					backShowLabel.setText(NumberFormat.getCurrencyInstance().format(paySumField.getValue() - sumTotal));
				} else {
					backShowLabel.setText(NumberFormat.getCurrencyInstance().format(0));
				}
			}
		});
		this.add(paySumField);

		this.add(backLabel);
		this.add(backShowLabel);

		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});

		this.add(cancelButton);

		
		confirmButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				if (sumTotal > paySumField.getValue()) {
					JOptionPane.showMessageDialog(getOwner(), "Too little!!!");
					return;
				}

				confirmed();
				setVisible(false);
			}
		});
		
		this.add(confirmButton);

		setSize(250, 120);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screen.width - getWidth()) / 2, (screen.height - getHeight()) / 2);
	}

	private void confirmed() {
		cancel = false;
	}

	public boolean showDialogIsCanceled() {
		setVisible(true);
		return cancel;
	}
}
